<?php
var_dump(111);die;
//引入初始化文件
include '../../init.php';
//引入数据库连接文件
include DIR_CORE .'mysql.php';
//接受数据
$user = trim($_POST['username']);
$password = trim($_POST['password']);
$captcha = trim($_POST['captcha']);
$admin_id = 2;
if ($user == 'root') {
    $admin_id = 1;
}
//判断数据合法性
//验证 验证码
session_start();
if (strtolower($captcha) != strtolower($_SESSION['captcha'])) {
    jump('../../view/admin/login.html', '验证码不正确！');
}
    //验证用户名
    if (empty($user)||empty($password)) {
        jump('../../view/admin/login.html', '用户名与密码不能为空！');
    }
    //连接数据库取数据验证
    $sql = "select * from user where name='$user' and admin_id='$admin_id'";
    //echo $sql;
    $res = mysqli_query($link, $sql);
    $row = mysqli_fetch_assoc($res);
    
    if (mysqli_affected_rows($link)==0) {
        jump('../../view/admin/login.html', '你不是管理员账号或未注册！');
    }
    if (md5($password)===$row['password']) {
        //验证成功
        setCookie('user_id', $row['id'], 0, '/');
        session_start();
        //建立会话区，将信息存在session中
        $_SESSION['userInfo']=$row;
      
        //登入信息
        $count = 1;
        $u_id = $row['id'];
        $ip = $_SERVER['REMOTE_ADDR'];
        $date = time();
        //查询是否第一次登入，是执行插入语句，否执行更新语句
        $l_sql = "select *from login where u_id = '$u_id'";
        $l = mysqli_query($link, $l_sql);
        $lrow = mysqli_fetch_assoc($l);
        if (!empty($lrow)) {
            //$lrow = mysqli_fetch_assoc($l);
            $count = $lrow['count']+1;
            $u_sql = "update login set login_ip='$ip',login_time=$date,count=$count where u_id=$u_id";
            //echo $u_sql;
            //die;
            $u_result = mysqli_query($link, $u_sql);
            //var_dump($u_result);
            if ($u_result) {
                jump('../../model/admin/index.php');
            } else {
                echo '插入有误1';
            }
        } else {
            $sql = "insert into login values(null,'$ip','$date','$count','$u_id')";
            //echo $sql;
            //die;
            $result = mysqli_query($link, $sql);
            if ($result) {
                jump('../../model/admin/index.php');
            } else {
                echo '插入有误';
            }
        }
    } else {
        jump('../../view/admin/login.html', '密码错误！');
    }
